//package wmmd.disc.webmaster;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import db.DatabaseConnector;

public class SearchUser extends HttpServlet {
    
    //----------------------------------------------------------------------------------------
    /**
     * Called by search.jsp; will find all messages by a user and display them
     * in chronological order.
     */
    
//doPost method included for POST requests
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        doGet(request, response);
    }
    
//main doGet method
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        //The JSP document passes in which db table to use
        String tablename = request.getParameter("tName");
        int ds = 0;   //for parsing the date string
        
        //Variables for the database connection
        DatabaseConnector con = new DatabaseConnector(DatabaseConnector.getDatabaseType());
        ResultSet rs = null;
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        try {
            //Grab the searchname, and assign to a string
            String search_string = request.getParameter("searchname");
            
            //If the Search Name field is not empty
            if(search_string != null && !search_string.equalsIgnoreCase("null") && search_string.length() > 0) {
                //Get all topics and responses by searchname
                String SQL = "SELECT ID,TLevel,Name,Date,Subject FROM " + tablename + " WHERE Topic IN ( SELECT Topic FROM "
                        + tablename + " WHERE Name = ?) ORDER BY Topic, ResponseID, Name, Date, TLevel, Subject, ID";
                rs = con.selectSQL_RS(SQL, new Object[]{search_string});
                
                out.print("<B><CENTER>1. Here are all topics and responses posted by ");
                out.println(search_string + "</B></CENTER><BR>");
                
                //Write them out in proper form; if the message is not a main topic,
                //indent it when it's written out
                if(rs != null)
                {
                    while(rs.next()) {
                        String id_string = rs.getString("ID");
                        int topic_level = Integer.valueOf(rs.getString("TLevel")).intValue();
                        String name_string = rs.getString("Name");
                        String date_string = rs.getString("Date");
                        String subject_string = rs.getString("Subject");
                        ds = date_string.indexOf(" ");
                    
                        if (topic_level > 1) {
                            for(int j=1; j < topic_level; j++) {
                                out.print("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                            }
                        }
                        out.print("<A href=\"displaymessage.jsp?RTID=");
                        out.println(id_string + "\">" + subject_string + "</A>" + "<BR>");
                        if (topic_level > 1) {
                            for(int j=1; j < topic_level; j++) {
                                out.print("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                            }
                        }
                        out.println("Posted by: <B>" + name_string + "</B>");
                        out.println("on " + date_string.substring(0, ds) + "<BR>");
                    }
                    rs.close();
                }
            }
        } catch (SQLException e) {
            out.println("SQLException caught: " + e.getMessage());
        } finally {
            try {
                if(rs != null) rs.close();
                if(con != null) con.CleanUp();
            } catch (Exception ignored) {}
        }
    }
}
